<template>
  <div>
    <van-goods-action>
        <van-goods-action-icon icon="chat-o" text="客服" />
        <van-goods-action-icon icon="cart-o" text="购物车" :to="{path:'/car'}" :badge="num" />
        <van-goods-action-icon icon="shop-o" text="店铺"/>
        <van-goods-action-button type="warning" text="加入购物车" @click="shop" />
        <van-goods-action-button type="danger" text="立即购买" @click="shop"/>
    </van-goods-action>
    <van-sku
        v-model="show"
        :sku="sku"
        :goods="goods"
        :goods-id="goodsId"
        :hide-stock="sku.hide_stock"
        @buy-clicked="onBuyClicked"
        @add-cart="onAddCartClicked"
        />

  </div>
</template>

<script>
// 导入请求
import {getCarNum,carAdd} from "@/https/http"

export default {    
    data() {
        return {
            num:0,
            show:false,
            sku:{
                list:[],
                tree:[],
                hide_stock:false,
                price:'',
                stock_num:0
            },
            goods:{
                picture:''
            },
            goodsId:''
        }
    },
    props:['info','productList'],
    methods:{
        shop(){
            this.show = true
        },
        onBuyClicked(){},
        // 点击购物车
        onAddCartClicked(res){
            if(localStorage.getItem('token')){
                let obj = {
                    goodsId:this.$route.query.id,
                    productId:this.productList[0].id,
                    number:res.selectedNum
                }
                carAdd(obj).then(val=>{
                    this.show = false
                    this.$toast.success('添加成功');
                    this.num = val.data.data.cartTotal.goodsCount
                })
            }else{
                this.$router.push('/car')
            }
        }
    },
    mounted(){

        this.goods.picture = this.info.list_pic_url
        this.goodsId = this.info.id
        this.sku.price = this.info.retail_price
        this.sku.stock_num = this.info.goods_number


        let id = this.$route.query.id
        console.log(id);
        if (localStorage.getItem('token')) {
            getCarNum().then(res=>{
                this.num = res.data.data.cartTotal.goodsCount
            })
        }else{
            this.num = ''
        }
    }
}
</script>

<style>

</style>